﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using qltscd.Models;
using qltscd.Models.ScreenClient;
using qltscd.Contansts;
using Telerik.Web.Mvc;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.IO;
using qltscd.Helper;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Web.Routing;
using qltscd.Views.DanhMucThietBi.Models;

namespace qltscd.Controllers
{
    public class PhieuBaoHanhController : Controller
    {
        //
        // GET: /SuaChua/

        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            if (SessionHelper.QuyenNhom[qltscd.Contansts.KeyContansts.QuyenTacVu] == '0')
            {
                filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new
                {
                    controller = "KhongCoQuyen",
                    action = "Index"
                }));
            }
        }

        private QLTBEntities db = new QLTBEntities();

        public ActionResult Index()
        {
            Models.BaseClient.PhieuBaoHanh baohanh = new Models.BaseClient.PhieuBaoHanh();
            ViewBag.SerialThietBiBaoHanh = new SelectList(db.DanhMucThietBis.OrderBy(i => i.IdentityThietBi), "IdentityThietBi", "SerialThietBi");
            ViewBag.NguoiGiao = TaoDanhSachNguoiDung(null);
            ViewBag.DonViBaoHanh = new SelectList(db.DanhMucNhaCungCaps.OrderBy(i => i.IdentityNhaCungCap), "IdentityNhaCungCap", "TenNhaCungCap");
            //ViewBag.ThayThe = new SelectList(db.thay)
            return View(baohanh);
        }

        [HttpPost]
        public ActionResult Index(Models.BaseClient.PhieuBaoHanh phieubaohanh)
        {
            PhieuBaoHanh pbh = new PhieuBaoHanh();
            if (ModelState.IsValid)
            {
                if (phieubaohanh.MaPhieuBaoHanh != pbh.MaPhieuBaoHanh)
                {
                    if (db.PhieuBaoHanhs.Count(o => o.MaPhieuBaoHanh == phieubaohanh.MaPhieuBaoHanh) > 0)
                    {
                        ViewData["errMaPhieuBaoHanh"] = "Mã phiếu bảo hành " + phieubaohanh.MaPhieuBaoHanh + " đã tồn tại";
                        ViewBag.SerialThietBiBaoHanh = new SelectList(db.DanhMucThietBis.OrderBy(i => i.IdentityThietBi), "IdentityThietBi", "SerialThietBi", phieubaohanh.IdentityThietBi);
                        ViewBag.NguoiGiao = TaoDanhSachNguoiDung(phieubaohanh.NguoiGiao);
                        ViewBag.DonViBaoHanh = new SelectList(db.DanhMucNhaCungCaps.OrderBy(i => i.IdentityNhaCungCap), "IdentityNhaCungCap", "TenNhaCungCap", phieubaohanh.DonViBaoHanh);
                        return View(phieubaohanh);
                    }
                }
                ObjectHelper.Copy(phieubaohanh, pbh);
                db.PhieuBaoHanhs.AddObject(pbh);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.SerialThietBiBaoHanh = new SelectList(db.DanhMucThietBis.OrderBy(i => i.IdentityThietBi), "IdentityThietBi", "SerialThietBi", phieubaohanh.IdentityThietBi);
            ViewBag.NguoiGiao = TaoDanhSachNguoiDung(phieubaohanh.NguoiGiao);
            ViewBag.DonViBaoHanh = new SelectList(db.DanhMucNhaCungCaps.OrderBy(i => i.IdentityNhaCungCap), "IdentityNhaCungCap", "TenNhaCungCap", phieubaohanh.DonViBaoHanh);
            return View(phieubaohanh);
        }

        private SelectList TaoDanhSachNguoiDung (int? selected) {
            List<DanhMucNguoiDung> List = db.DanhMucNguoiDungs.OrderBy(i => i.TenNguoiDung).ToList();
            List<SelectableNguoiDung> nds = new List<SelectableNguoiDung>();
            foreach (DanhMucNguoiDung nd in List) {
                nds.Add(new SelectableNguoiDung(nd.IdentityNguoiDung, nd.HoTenDem, nd.TenNguoiDung));
            }
            return new SelectList(nds, "IdentityNguoiDung", "HoTenNguoiDung", selected);
        }
    }
}
